package com.sunny.ninepictureview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * 按下会有灰色遮罩的ImageView
 * Created by Sunny  An on 2016/5/23.
 */
public class PressGrayImageView extends ImageView {

    //边框大小
    private static final int BORDER_WIDTH = 2;

    private Paint paint;
    private float drawWidth = 0;
    private float drawHeight = 0;


    public PressGrayImageView(Context context) {
        super(context);
        init();
    }

    public PressGrayImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PressGrayImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.LTGRAY);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(BORDER_WIDTH);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Drawable drawable;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                drawable = getDrawable();
                if (drawable != null) {
                    drawable.mutate().setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY);
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                drawable = getDrawable();
                if (drawable != null) {
                    drawable.mutate().clearColorFilter();
                }

        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        //setImageDrawable(null);
        //setImageBitmap(null);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        switch (getScaleType()) {
            case CENTER_CROP:
                canvas.drawRect(BORDER_WIDTH / 2, BORDER_WIDTH / 2, getWidth() - BORDER_WIDTH / 2, getHeight() - BORDER_WIDTH / 2, paint);
                break;
            case FIT_START:
                getImageWidthAndHeight();
                canvas.drawRect(BORDER_WIDTH / 2, BORDER_WIDTH / 2, drawWidth - BORDER_WIDTH / 2, drawHeight - BORDER_WIDTH / 2, paint);
                break;
        }
    }


    /**
     * 获取image图片绘制的宽高
     */
    private void getImageWidthAndHeight() {
        Drawable drawable = getDrawable();
        Matrix matrix = getImageMatrix();
        if (drawable != null && matrix != null) {
            Rect rect = drawable.getBounds();
            int readWidth = rect.width();
            int readHeight = rect.height();

            //获得ImageView中Image的变换矩阵;
            float[] values = new float[9];
            matrix.getValues(values);

            float scaleX = values[0];
            float scaleY = values[4];

            //计算Image在屏幕上实际绘制的宽高
            drawWidth = readWidth * scaleX;
            drawHeight = readHeight * scaleY;

        }
    }
}
